var t = getApp(), e = t.requirejs("core"), i = t.requirejs("foxui");
Page({
  data: {
    payList: [],
    images: [],
    imgs: [],
    len: 1,
    merch: {},
    money: "",
    payment: "",
    shanghuid: 0
  },
  onLoad: function(p) {
    var merchid = 0;
    if(p.merchid) merchid = p.merchid, console.log(t);
    if(p.scene){
        var pt = decodeURIComponent(p.scene);
        var scene = e.str2Obj(pt);
        merchid = scene.merchid;
        console.log(p.scene);
    }
    this.setData({
      shanghuid: merchid
    })
  },
  onShow: function() {
    this.setData({
      imgUrl: t.globalData.approot,
    })
    var userinfoOpenid = t.getCache("userinfo_openid");
    if(!userinfoOpenid) {
      wx.navigateTo({
        url: "/pages/auth/index",
      })
      return false;
    }
    this.getInfo();
  },
  getInfo: function() {
    var t = this;
    e.get("merch/offline", {shanghuid:t.data.shanghuid}, function(r) {
      console.log(r);
      t.setData({
        merch: r.merch,
        payList: r.pay_type,
        show: !0,
      })
    });
  },
  radioChange: function(e) {
    this.setData({
      applyid: e.detail.value
    })
  },
  upload: function (t) {
    var that = this,
      s = e.data(t),
      i = s.type,
      o = that.data.images,
      n = that.data.imgs,
      r = s.index;
    "image" == i ? e.upload(function (t) {
      o.push(t.filename), n.push(t.url), that.setData({
        images: o,
        imgs: n,
        len: 0
      })
    }) : "image-remove" == i ? (o.splice(r, 1), n.splice(r, 1), that.setData({
      images: o,
      imgs: n,
      len: 1
    })) : "image-preview" == i && wx.previewImage({
      current: n[r],
      urls: n
    })
  },
  bindPayment: function (v) {
    var s = v.detail.value;
    var value = this.data.payList[s].value, 
    title = this.data.payList[s].title;
    this.setData({
      payment: value,
      payname: title,
    })
  },
  paytype(e) {
    this.setData({navlist: e.currentTarget.dataset.type});
  },
  onMoney: function(p) {
    let money = p.detail.value;
    this.setData({
      money: money
    })
  },
  submit: function () {
    var userinfoOpenid = t.getCache("userinfo_openid");
    if(!userinfoOpenid) {
      wx.navigateTo({
        url: "/pages/auth/index",
      })
      return false;
    }
    var o = this, td = o.data;
    if (!o.data.submit) {
      var money = td.money, payment = td.payment, shid = td.shanghuid, c = {};
      if(!money) {
        i.toast(o, "请输入消费金额");
        return false;
      }
      if(!payment) {
        i.toast(o, "请选择支付方式");
        return false;
      }
      c.money = money,
      c.type = payment,
      c.shanghuid = shid;
      var a = payment;

      e.confirm("确认要支付吗?", function() {
        e.post("merch/offline/submit", c, function (r) {
          if(a=="merchcard"){
            if(0 == r.error) 
              e.alert("支付成功"),
              o.setData({
                payment: "",
                payname: "",
                money: "",
              }), o.getInfo();
            else
              e.alert("支付成功");
          }else{
            0 == r.error ? r.wechat.success ? e.pay(r.wechat.payinfo, function(a) {
              "requestPayment:ok" == a.errMsg && e.post("merch/offline/wechat_complete", {
                logid: r.logid
              }, function(r) {
                if (0 == r.error) {
                  e.alert("支付成功"),
                  o.setData({
                    payment: "",
                    payname: "",
                    money: "",
                  }), o.getInfo();
                } else e.alert("支付失败");
              }, !0);
            }) : e.alert("\n不能使用微信支付!") : e.alert(r.message);
          }
        })
      })
    }
  },
});